package com.jason.service;

import com.jason.api.CodeService;
import com.jason.api.vo.Response;
import com.jason.constant.ResponseConstant;
import com.jason.entity.Token;
import com.jason.entity.User;
import com.jason.mapper.TokenMapper;
import com.jason.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenMapper tokenMapper;

    @Reference(timeout = 100000)
    private CodeService codeService;

    private final String COOKIE_LOGIN = "cookieLogin";

    private final String COOKIE_USER = "cookieUser";

    public void register(String email, String password, String confirmPassword, String code) {
        if (StringUtils.isEmpty(email))
            throw new RuntimeException("请输入邮箱");
        if (StringUtils.isEmpty(password))
            throw new RuntimeException("请输入密码");
        if (StringUtils.isEmpty(confirmPassword))
            throw new RuntimeException("请输入确认密码");
        if (!password.equals(confirmPassword))
            throw new RuntimeException("密码与确认密码不一致");
        if (StringUtils.isEmpty(code))
            throw new RuntimeException("验证码为空");

        Response response = codeService.checkCode(email, code);
        if (ResponseConstant.SUCCESS.intValue() != response.getCode().intValue())
            throw new RuntimeException(response.getMessage());

        //验证邮箱是否注册过
        if (!ObjectUtils.isEmpty(selectByEmail(email)))
            throw new RuntimeException("该邮箱已经注册过，请确认");

        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        userMapper.insertSelective(user);

    }

    public User selectByEmail(String email) {
        User user = new User();
        user.setEmail(email);
        return userMapper.selectOne(user);
    }

    public void login(String email, String password, HttpServletResponse response) {
        if (StringUtils.isEmpty(email))
            throw new RuntimeException("请输入邮箱");
        if (StringUtils.isEmpty(password))
            throw new RuntimeException("请输入密码");
        User user = selectByEmail(email);
        if (ObjectUtils.isEmpty(user))
            throw new RuntimeException("该邮箱未注册");
        if (!password.equals(user.getPassword()))
            throw new RuntimeException("密码输入不正确，请确认");

        //生成token令牌
        String tokenCode = UUID.randomUUID().toString();
        Cookie cookie = new Cookie(COOKIE_LOGIN, tokenCode);
        cookie.setPath("/");
        cookie.setSecure(Boolean.FALSE);
        response.addCookie(cookie);

        Token token = new Token();
        token.setEmail(email);
        token.setToken(tokenCode);
        tokenMapper.insertSelective(token);
    }

    public User getUserInfo(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (COOKIE_LOGIN.equals(cookie.getName())) {
                    Token token = getByTokenCode(cookie.getValue());
                    String email = token.getEmail();
                    return selectByEmail(email);
                }
            }
        }
        return new User();
    }

    public Token getByTokenCode(String tokeCode) {
        Token token = new Token();
        token.setToken(tokeCode);
        return tokenMapper.selectOne(token);
    }
}
